package utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.persistence.Entity;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

import controllers.Application;

public class POIExcelReader {
	
//	public Map<Integer,List<String>> dataRecovered;
//	public Map<Integer,List<String>> erreurFile;
//	public static List<String> erreurs = new ArrayList<String>();
	

//	public POIExcelReader() {
//		this.dataRecovered = new HashMap<Integer, List<String>>();
//		this.erreurFile = new HashMap<Integer, List<String>>();
//	}	
//
	public static String getErreurs(List<String> erreurs) {
		StringBuffer sb = new StringBuffer();
		
		if(!erreurs.isEmpty()){
			for(int i = 0; i < erreurs.size() ; i++){
				sb.append("["+i+"] : "+erreurs.get(i)+"<br>");
			}
		}
		return sb.toString();
	}

	/**
	 * This method is used to display the Excel content to command line.
	 * 
	 * @param xlsPath
	 * @return 
	 */
	public static Map<Integer, List<String>> getDataFromExcel(File importFile) {
		Map<Integer,List<String>> dataRecovered;
		InputStream inputStream = null;
	
		try {
			inputStream = new FileInputStream(importFile);
		} catch (FileNotFoundException e) {
			System.out.println("File not found in the specified path.");
			e.printStackTrace();
		}

		POIFSFileSystem fileSystem = null;
		dataRecovered = new HashMap<Integer, List<String>>();
		
		try {
			fileSystem = new POIFSFileSystem(inputStream);
			

			HSSFWorkbook workBook = new HSSFWorkbook(fileSystem);
			HSSFSheet sheet = workBook.getSheetAt(0);			
			
			Iterator<Row> rows = sheet.rowIterator();
			
			while (rows.hasNext()) {
				List<String> dataLine= null;
				
				Row row = rows.next();
				
				if(row.getRowNum() == 0) continue;
				
				dataLine = getDataFromRow(row);
				dataRecovered.put(row.getRowNum(), dataLine);				
			}	
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			try {
				inputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		return dataRecovered;
	}
	
	public static String checkDataRecovered(List<String> dataToCheck) throws IOException{
		List<ConfigurationFile> configurationFile = null;
		List<String> erreurs = new ArrayList<String>();
		configurationFile = ConfigurationFile.initConfigurationFile();
		
		erreurs = verifyDataConfig(configurationFile, dataToCheck);
		if(erreurs.isEmpty()){
			erreurs = verifyData(configurationFile, dataToCheck);
		}	
		return getErreurs(erreurs);
	}
	
	private static List<String> verifyData(List<ConfigurationFile> configurationFile,
			List<String> datas) {
		List<String> erreur = new ArrayList<String>();
		ControlData controleData = new ControlData();
		if(!controleData.verifyDates(datas.get(3), datas.get(4)))
			erreur.add(controleData.errorFind);
		return erreur;
	}

	private static List<String> verifyDataConfig(List<ConfigurationFile> configurationFile, List<String> datas) {
		List<String> erreurs = new ArrayList<String>();
		int i = 0;
		for (String value : datas){
			ConfigurationFile configData = configurationFile.get(i);
			ControleFile controleFile = new ControleFile(configData.getChamps(), value, configData.getMaxsize(), 
						configData.isRequired(), configData.getType());
			if(!controleFile.controlValue()){
				erreurs.add(controleFile.errorFind);
			}
			i++;
		}
		return erreurs;		
	}



	private static List<String> getDataFromRow(Row row){
		List<String> data = new ArrayList<String>(6);
		
		for(int j = 0; j < 6 ; j++){
			HSSFCell cell = (HSSFCell) row.getCell(j);
			if(cell == null){
				data.add(null);
			}else{
				switch (cell.getCellType()) {
					case HSSFCell.CELL_TYPE_NUMERIC: {
						if(HSSFDateUtil.isCellDateFormatted(cell)){
					        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
					        data.add(sdf.format(cell.getDateCellValue()));
						}else{
							data.add(String.valueOf((int)cell.getNumericCellValue()));
						}
						break;
					}
					
					case HSSFCell.CELL_TYPE_STRING: {
						data.add(cell.getRichStringCellValue().getString());
						break;
					}
		
					default: {
						data.add("");
						System.out.println("Type not supported.");
						break;
					}
				}
		}
		}	
		return data;
	}

}
